<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <el-card class="card">
            <el-button type="primary" class="btn" @click="addData"
              ><i class="el-icon-plus"></i> 新建角色</el-button
            >
            <!-- 表格 -->
            <el-table :data="tableData" style="width: 100%">
              <el-table-column label="序号" width="180" type="index">
              </el-table-column>
              <el-table-column prop="name" label="角色名称" width="180">
              </el-table-column>
              <el-table-column prop="description" label="描述">
              </el-table-column>
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="openEdit(row)">修改</el-button>
                  <el-button type="text" @click="delData(row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
              class="pagination"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="pagesizes"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="tableTotal"
            >
            </el-pagination>
          </el-card>

          <!-- dialog对话框 -->
          <el-dialog :title="title" :visible="dialogShow" @close="closeDialog">
            <el-form :model="form" label-width="100px">
              <el-form-item label="角色名称">
                <el-input v-model="form.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="描述">
                <el-input
                  v-model="form.description"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="closeDialog">取 消</el-button>
              <el-button type="primary" @click="updateData">确 定</el-button>
            </div>
          </el-dialog>
        </el-tab-pane>
        <el-tab-pane label="公司设置" name="second">
          <el-card class="card">
            <el-alert
              :title="warning"
              type="warning"
              show-icon
              :closable="false"
            >
            </el-alert>
            <div class="box">
              <div>
                <span>公司名称</span>
                <el-input v-model="companyInfo.name" disabled> </el-input>
              </div>
              <div>
                <span>公司地址</span>
                <el-input v-model="companyInfo.companyAddress" disabled> </el-input>
              </div>
              <div>
                <span>公司电话</span>
                <el-input v-model="companyInfo.companyPhone" disabled> </el-input>
              </div>
              <div>
                <span>邮箱</span>
                <el-input v-model="companyInfo.mailbox" disabled> </el-input>
              </div>
              <div>
              <span>备注</span>
              <el-input
                type="textarea"
                v-model="companyInfo.remarks"
                disabled
              >
              </el-input>
            </div>
            </div>
            
          </el-card>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {
  getSettings,
  updateSetting,
  addSetting,
  deleteSetting,
  getCompanyInfo
} from '@/api/settings'
export default {
  mounted () {
    this.getSetting(this.pagesize, this.currentPage)
    this.getCompany()
  },
  data () {
    return {
      activeName: 'first',
      tableData: [],
      tableTotal: 1,
      pagesizes: [2, 4, 6, 8],
      pagesize: 2,
      currentPage: 1,
      title: '',
      dialogShow: false,
      form: {
        name: '',
        description: ''
      },
      currentItem: {},
      // 公司信息
      warning:
        '对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改',
      companyInfo: {}
    }
  },
  methods: {
    // 设置分页功能
    handleSizeChange (val) {
      this.getSetting(val, this.currentPage)
      this.pagesize = val
      console.log(val)
    },
    handleCurrentChange (val) {
      // console.log(val);
      this.getSetting(this.pagesize, val)
      this.currentPage = val
    },
    // 获取角色列表
    async getSetting (ps, page) {
      let res = await getSettings({ pagesize: ps, page: page })
      // console.log(res)
      this.tableData = res.data.rows
      this.tableTotal = res.data.total
    },
    // 编辑页
    openEdit (obj) {
      console.log(obj)
      this.currentItem = obj
      this.dialogShow = true
      this.form = { ...obj }
      this.title = '编辑页面'
    },
    // 关闭页面
    closeDialog () {
      ;(this.dialogShow = false),
        (this.form = {
          name: '',
          description: ''
        })
    },
    // dialog页面
    async updateData () {
      let res = null
      if (this.form.id) {
        res = await updateSetting(this.form)
      } else {
        res = await addSetting(this.form)
      }
      this.dialogShow = false
      this.$message.success(res.message)
      this.form = {
        name: '',
        description: ''
      }
      await this.getSetting(this.pagesize, this.currentPage)
    },
    // 新增页
    addData () {
      this.dialogShow = true
      this.title = '新增页面'
    },
    // 删除页
    async delData (row) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteSetting(row.id)
        if (this.tableData.length === 1) {
          this.currentPage--
          await this.getSetting(this.pagesize, this.currentPage)
        } else {
          await this.getSetting(this.pagesize, this.currentPage)
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      } catch (error) {}
    },
    // 获取公司信息
    async getCompany(){
      let res = await getCompanyInfo()
      console.log(res);
      this.companyInfo = res.data[0]
    }
  }
}
</script>

<style lang="scss">
.card {
  padding: 40px 20px;
  .btn {
    margin-bottom: 30px;
  }
  .pagination {
    margin-top: 40px;
    text-align: right;
  }
  .box {
    margin-left: 20px;
    padding-top: 10px;
    width: 40%;
    div {
      display: flex;
      align-items: center;
      margin: 10px;
      span {
        width: 100px;
        font-size: 16px;
        font-weight: 700;
        color: #63676d;
      }
    }
  }
}
</style>
